package com.kuaimacode.kframework.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.mybatis.mapper.invite.UserInviteMapper;
import com.kuaimacode.kframework.mybatis.models.invite.UserInviteRecord;
import com.kuaimacode.kframework.mybatis.models.user.UserInvite;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 邀请记录
 * @author anzy
 * @date 2019/03/31
 */
@Service
public class UserInviteService extends ServiceImpl<UserInviteMapper, UserInvite> {

    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<UserInvite> findAll(int pageNum, int pageSize) {
    		PageHelper.startPage(pageNum, pageSize);
    		List<UserInvite> datas = baseMapper.selectList(null);
    		return new PageInfo<UserInvite>(datas);
    	}

		/**
		 * 查询某人的邀请记录
		 * @return
		 */
		public List<UserInvite> queryByUserId(Long userId, Long inviteId){
    		QueryWrapper queryWrapper = new QueryWrapper();
    		queryWrapper.eq("inviter", userId);
			Optional.ofNullable(inviteId).ifPresent(in->queryWrapper.eq("invite_id",inviteId));
    		return baseMapper.selectList(queryWrapper);
		}

		/**
		 * 查询某人的邀请记录
		 * @return
		 */
		public List<UserInviteRecord> findInviteRecordByUserId(Long userId, Long inviteId) {
			return baseMapper.findInviteRecordByUserId(userId, inviteId);
		}

}

